import { computed, readonly, ref } from 'vue'
import Client from '@/api/client/index.js'
import store from '@/store'

const backgroundInfo = {}
backgroundInfo.loaded = ref(false)
backgroundInfo.raw = ref({
  cbgDemandType: [],
  cbg: {}
})
backgroundInfo.transformed = ref({
  cbgDemandType: [],
  cbg: {}
})

export function useClientBackgroundInfo(id, resolve = () => {}, forceUpdate) {
  if (!backgroundInfo.loaded.value || forceUpdate) {
    getData(id, resolve)
  } else {
    resolve({
      raw: readonly(backgroundInfo.raw),
      transformed: readonly(backgroundInfo.transformed)
    })
  }
  const reload = getData.bind(this, id, resolve)
  return {
    backgroundInfo: {
      raw: readonly(backgroundInfo.raw),
      transformed: readonly(backgroundInfo.transformed),
      loaded: readonly(backgroundInfo.loaded)
    },
    reload
  }
}
function getData(id, resolve) {
  backgroundInfo.loaded.value = false
  Client.getBackgroundInfo(id).then(async ({ data }) => {
    backgroundInfo.raw.value = data
    const dict = ref(computed(() => store.state.dict))
    await store.dispatch('dict/getRivals')
    await store.dispatch('dict/getRivalLevels')
    await store.dispatch('dict/getClientValues')
    await store.dispatch('dict/getDemandTypes', { pid: 0, sub: true, force: true })
    await store.dispatch('dict/getForecastPOs')
    await store.dispatch('dict/getInternalLocalizationProcess')
    await store.dispatch('dict/getProcurementProcess')
    await store.dispatch('dict/getPainPoints')
    await store.dispatch('dict/getCommunicationChannels')
    await store.dispatch('dict/getCommunicationModes')
    await store.dispatch('dict/getReviewInfos')
    await store.dispatch('dict/getPlatforms')
    await store.dispatch('dict/getTranslationPurpose')
    await store.dispatch('dict/getQuotationStrategies')
    backgroundInfo.loaded.value = true
    resolve({
      raw: readonly(backgroundInfo.raw),
      transformed: readonly(backgroundInfo.transformed)
    })
    // 背景信息部分字典翻译
    if (backgroundInfo.raw.value.cbg) {
      const rival = dict.value.rivals.find(rival => rival.value === backgroundInfo.raw.value.cbg.rivalID)
      backgroundInfo.transformed.value.cbg.rivalID = rival ? rival.text : backgroundInfo.raw.value.cbg.rivalID

      const rivalLevel = dict.value.rivalLevels.find(
        rivalLevel => rivalLevel.value === backgroundInfo.raw.value.cbg.rivalLevelID
      )
      backgroundInfo.transformed.value.cbg.rivalLevelID = rivalLevel
        ? rivalLevel.text
        : backgroundInfo.raw.value.cbg.rivalLevelID

      const clientValue = dict.value.clientValues.find(
        clientValue => clientValue.value === backgroundInfo.raw.value.cbg.valueID
      )
      backgroundInfo.transformed.value.cbg.valueID = clientValue
        ? clientValue.text
        : backgroundInfo.raw.value.cbg.valueID
    }
    // 需求信息部分字典翻译
    backgroundInfo.transformed.value.cbgDemandType.splice(0)
    if (Array.isArray(data.cbgDemandType) && data.cbgDemandType.length > 0) {
      data.cbgDemandType.forEach((demand, index) => {
        backgroundInfo.transformed.value.cbgDemandType.push({})
        const demandType = dict.value.demandTypes.find(demandType => demandType.value === demand.demandTypeID)
        backgroundInfo.transformed.value.cbgDemandType[index].demandTypeID = demandType
          ? demandType.text
          : backgroundInfo.raw.value.cbgDemandType[index].demandTypeID

        if (
          Array.isArray(backgroundInfo.raw.value.cbgDemandType[index].demandContentID) &&
          backgroundInfo.raw.value.cbgDemandType[index].demandContentID.length > 0
        ) {
          backgroundInfo.transformed.value.cbgDemandType[index].demandContentID = []
          backgroundInfo.raw.value.cbgDemandType[index].demandContentID.forEach(demandContentID => {
            const demandType = dict.value.demandTypes.find(demandType => demandType.value === demandContentID)
            if (demandType) {
              backgroundInfo.transformed.value.cbgDemandType[index].demandContentID.push(demandType.text)
            }
          })
        }
        const forecastPO = dict.value.forecastPOs.find(forecastPO => forecastPO.value === demand.proPOID)
        backgroundInfo.transformed.value.cbgDemandType[index].proPOID = forecastPO
          ? forecastPO.text
          : backgroundInfo.raw.value.cbgDemandType[index].proPOID

        const iLProcess = dict.value.internalLocalizationProcess.find(iLProcess => iLProcess.value === demand.processID)
        backgroundInfo.transformed.value.cbgDemandType[index].processID = iLProcess
          ? iLProcess.text
          : backgroundInfo.raw.value.cbgDemandType[index].processID

        const procurementProcess = dict.value.procurementProcess.find(
          process => process.value === demand.purchaseModeID
        )
        backgroundInfo.transformed.value.cbgDemandType[index].purchaseModeID = procurementProcess
          ? procurementProcess.text
          : backgroundInfo.raw.value.cbgDemandType[index].purchaseModeID

        const painPoint = dict.value.painPoints.find(painPoint => painPoint.value === demand.notGoodID)
        backgroundInfo.transformed.value.cbgDemandType[index].notGoodID = painPoint
          ? painPoint.text
          : backgroundInfo.raw.value.cbgDemandType[index].notGoodID

        const channel = dict.value.communicationChannels.find(channel => channel.value === demand.talkChannelID)
        backgroundInfo.transformed.value.cbgDemandType[index].talkChannelID = channel
          ? channel.text
          : backgroundInfo.raw.value.cbgDemandType[index].talkChannelID

        const mode = dict.value.communicationModes.find(method => method.value === demand.talkModeID)
        backgroundInfo.transformed.value.cbgDemandType[index].talkModeID = mode
          ? mode.text
          : backgroundInfo.raw.value.cbgDemandType[index].talkModeID

        const platform = dict.value.platforms.find(platform => platform.value === demand.platformID)
        backgroundInfo.transformed.value.cbgDemandType[index].platformID = platform
          ? platform.text
          : backgroundInfo.raw.value.cbgDemandType[index].platformID

        const review = dict.value.reviewInfos.find(review => review.value === demand.aboutReviewID)
        backgroundInfo.transformed.value.cbgDemandType[index].aboutReviewID = review
          ? review.text
          : backgroundInfo.raw.value.cbgDemandType[index].aboutReviewID

        const tp = dict.value.translationPurpose.find(tp => tp.value === demand.useTranslationID)
        backgroundInfo.transformed.value.cbgDemandType[index].useTranslationID = tp
          ? tp.text
          : backgroundInfo.raw.value.cbgDemandType[index].useTranslationID

        const strategy = dict.value.quotationStrategies.find(strategy => strategy.value === demand.strategy)
        backgroundInfo.transformed.value.cbgDemandType[index].strategy = strategy
          ? strategy.text
          : backgroundInfo.raw.value.cbgDemandType[index].strategy
      })
    }
  })
}
